package application;

import application.dao.DBService;
import application.dao.entity.Manager;
import application.dao.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.ResourceBundle;

public class Manager_personal_information_controller extends BaseController implements Initializable {

    @FXML
    public TextField userName;
    @FXML
    public TextField password;
    @FXML
    public TextField realName;
    @FXML
    public PasswordField surePassword;
    @FXML
    public TextField idCard;
    @FXML
    public TextField mobile;
    @FXML
    public TextField address;
    @FXML
    private Button change_info;

    @FXML
    private Menu menu_join;

    @FXML
    private MenuItem menu_join_competitons;

    @FXML
    private Menu menu_main;

    @FXML
    private MenuItem menu_join_users;

    @FXML
    private Button enroll_yes;

    @FXML
    private Menu menu_person;

    @FXML
    private Menu menu_informations;

    @FXML
    private Menu menu_scores;

    @FXML
    void onButton_enroll_yes(ActionEvent event) {
        if (!surePassword.getText().equals(password.getText())) {
            BaseUtil.info("密码不一致");
            return;
        }
        Manager user = createUser();
        boolean b = DBService.updateManager(user);
        if (b) {
            BaseUtil.info("修改成功");
        } else {
            BaseUtil.gotoMain(b, "修改失败", stage);
        }
    }

    private Manager createUser() {
        Manager loginUser = Global.loginAdmin;
        loginUser.setPassWord(password.getText());
        loginUser.setTelNumber(mobile.getText());
        loginUser.setHomeAddress(address.getText());
        return Global.loginAdmin;
    }

    @FXML
    void onButton_change_info(ActionEvent event) {
        System.out.println("change");
        clearData();
    }

    private void clearData() {
        BaseUtil.clearText(userName, realName, idCard, mobile, address, password, surePassword);
    }

    @FXML
    void onCheckMenu_main(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_person(ActionEvent event) {

    }

    @FXML
    void menu_join(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_users(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_competitons(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_scores(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initData();
    }

    private void initData() {
        Manager loginAdmin = Global.loginAdmin;
        userName.setText(loginAdmin.getUserName());
        realName.setText(loginAdmin.getRealName());
        idCard.setText(loginAdmin.getIdCard());
        mobile.setText(loginAdmin.getTelNumber());
        address.setText(loginAdmin.getHomeAddress());
        String passWord = loginAdmin.getPassWord();
        password.setText(passWord);
        surePassword.setText(passWord);
    }


    @Override
    public void init() {

    }
}